﻿using Server.Constant;
using Server.Enum;
using Server.Framework.Message;
using Server.Framework.Processor;
using Server.Framework.Service;
using Server.Framework.Socket;
using Server.Message.Request;
using Server.Message.Response;
using Server.Model;
using Server.OptExp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Server.Processor
{
    /// <summary>
    /// Author: Leil
    /// Description: 顶级菜单列表请求处理
    /// CreateTime: 2020/5/31 1:09:15
    /// </summary>
    [ProcessorAttr(MessageId.REQUEST_ORG_TREE)]
    class RequestOrgTreeProcessor : IProcessor
    {
        public void Process(ProtobufAppSession session, RequestBaseMessage message)
        {
            if (message is OrgTreeReq msg)
            {
                string sql = "SELECT * FROM SYS_ORG WHERE DEL_FLAG = 1 AND STATUS = 1 ORDER BY SORT DESC";
                try
                {
                    DBService.Instance().ExecuteQuery(sql, reader =>
                    {
                        OrgTreeResp resp = new OrgTreeResp(MessageId.RESPONSE_ORG_TREE)
                        {
                            HasRoot = msg.HasRoot
                        };
                        while (reader.Read())
                        {
                            OrgDao org = new OrgDao()
                            {
                                ID = Convert.ToInt32(reader["ID"]),
                                ParentId = Convert.ToInt32(reader["PARENT_ID"]),
                                OrgName = reader["ORG_NAME"].ToString()
                            };
                            resp.Orgs.Add(org);
                        }
                        session.Send(resp);
                    });
                }
                catch (CommonOptException e)
                {
                    Console.WriteLine(e.Message);
                }


            }
        }






    }
}
